﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Security.Cryptography;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;
using CoreShop.App;
using CoreShop.Library;
using CoreShop.Services;
using System.Windows.Forms;
using CoreShop.Model;
using CoreShop.PrintService;
using SqlSugar;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
using Yitter.IdGenerator;
using SqlSugar.Extensions;
using FastMember;
using Sunny.UI;
using Sunny.UI.Win32;
using System.Data;
using System.Runtime.InteropServices;
using E3.CSharpZipLib.Zip.Compression.Streams;

namespace CoreShop
{
    public class DahuiUtil
    {
        [DllImport("DhScalePluNet.dll")]
        public static extern int dhSendPluDefault();

        public static corecmsscale scaleModel = new corecmsscale();
        public static List<corecmsfoodspec> goodsList = new List<corecmsfoodspec>();

        public static void SaveDahuaIP(string strIP)
        {
            string sIP = string.Format("{0}	3", strIP);

            //写错误到日志文件
            try
            {
                string fileName = Application.StartupPath + @"\dhip.ini";
                StreamWriter sw0 = new StreamWriter(fileName);
                sw0.Write(sIP);
                sw0.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static bool SaveDahuaPlu()
        {
            string fileName = Application.StartupPath + @"\temp\Pro_Plu.txt";
            FileStream fs = new FileStream(fileName, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);

            decimal dPrice = 0;
            string sTemp = "";
            string sCodeID = "";
            string sNumber = "";
            string sName = "";
            string sPrice = "";
            string sSaveDay = "";
            string sDataFormat = "";
            string sRowFormat = "";
            string sBalance = AppConstVars.systemsetup.weighingCodeSign + AppConstVars.systemsetup.weighingCodeSign;
            string sPriceMode = "0";
            try
            {
                //开始写入,1P 0001 A 2100001 001500 0000000 030 21210000000000000000000002000000000000000000000000##五花肉##五花肉##五花肉
                //sDataFormat = "1P 序号 A 编码    单价   0000000 保  部门部门0000000000000000000002000000000000000000000000##五花肉##五花肉##五花肉";
                //sDataFormat = "1P{0}A{1}{2}0000000{3}{4}0000000000000000000002000000000000000000000000##{5}##{5}##{5}";
                sDataFormat = "1P{0}A{1}{2}{6}000000{3}{4}0000000000000000000002000000000000000000000000##{5}##{5}##{5}";

                foreach (var item in goodsList)
                {
                    //sNumber = item.number;
                    sNumber = AppConstVars.systemsetup.weighingCodeSign + item.weighPlu.ToString().PadLeft(5, '0');
                    sTemp = item.weighPlu.ToString();
                    sCodeID = sTemp.PadLeft(4, '0');                    
                    dPrice = item.posPrice * 100;
                    if (AppConstVars.systemsetup.paraPartialModel.scaleConvert == 1)
                        dPrice = item.posPrice * 100 * 2;
                    sPrice = Math.Floor(dPrice).ToString();
                    sPrice = sPrice.PadLeft(6, '0');
                    sTemp = item.periodDay.ToString();
                    sSaveDay = sTemp.PadLeft(3, '0');
                    sName = item.name;
                    sPriceMode = "0";
                    if (item.stand == (int)GlobalEnumVars.stand.Count) sPriceMode = "1";//计件
                    sRowFormat = string.Format(sDataFormat, sCodeID, sNumber, sPrice, sSaveDay, sBalance, sName, sPriceMode) + "\r\n";
                    sw.Write(sRowFormat);
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
            finally
            {
                //清空缓冲区
                sw.Flush();
                //关闭流
                sw.Close();
                fs.Close();
            }
        }


        public static void SendToScales_DH(UIForm form, string strIP, corecmsscale _scaleModel,List<corecmsfoodspec> _goodsList)
        {
            goodsList = _goodsList;
            scaleModel = _scaleModel;
            try
            {
                if (!new PingPrinter().PingIPAddress(strIP))
                    if (!form.ShowAskDialog("连接秤无响应，要继续下传吗？")) return;

                SaveDahuaIP(strIP);
                if (!SaveDahuaPlu()) return;
                int ret = dhSendPluDefault();
                switch (ret)
                {
                    case 0:
                        UIMessageBox.Show("下传成功.");
                        break;
                    case -1:
                        UIMessageBox.Show("连接失败.");
                        break;
                    case -2:
                        UIMessageBox.Show("通讯失败(服务器无响应，或服务器返回数据错误)！");
                        break;
                    case -3:
                        UIMessageBox.Show("发送失败(写发送缓冲失败)！");
                        break;
                    case -4:
                        UIMessageBox.Show("连接断开！");
                        break;
                    case -5:
                        UIMessageBox.Show("待发送数据为空！");
                        break;
                    case -6:
                        UIMessageBox.Show("用户手动退出！");
                        break;
                    case -7:
                        UIMessageBox.Show("目标IP为空！");
                        break;
                    case -9:
                        UIMessageBox.Show("通讯未全部成功，某IP出错，具体信息保存在return.txt中 ！");
                        break;
                    case -12:
                        UIMessageBox.Show("dhpluversion.int为空或设置错误！");
                        break;
                    case -13:
                        UIMessageBox.Show("dhplupathname.ini为空或设置错误！");
                        break;
                    case -14:
                        UIMessageBox.Show("dhplupathname.ini中读取的Plu路径不对,所指向的Plu文件不存在！");
                        break;
                    case -15:
                        UIMessageBox.Show("Plu文本为空或者设置错误(如:Plu版本与Plu文本内容不一致等)！");
                        break;
                }
            }
            catch (Exception ex)
            {
                UIMessageBox.Show(ex.Message);
            }
        }

    }
}
